package com.hsy.baoinbao.modules.order.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.ui.adapter.DPBaseAdapter;
import com.deparse.dpframe.utils.DPGsonUtil;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.deparse.dpframe.utils.DPLogUtil;
import com.hsy.baoinbao.R;
import com.hsy.baoinbao.common.Consts;
import com.hsy.baoinbao.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.baoinbao.modules.order.adapter.GoodsAllAdapter;
import com.hsy.baoinbao.modules.order.gsondata.GoodsOrderDetail;
import com.hsy.baoinbao.utils.CommonUtils;
import com.hsy.baoinbao.utils.HttpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/3/27.
 */
public class DetailGoodsActivity extends CommonBaseActivityWithSimpleActionBar {
    private ListView goods_list;
    private DetailGoodsAdapter detailGoodsAdapter;
    private List<HashMap<String, String>> ListData;
    private TextView contact_name_text, contact_phone_text, address_text, total_price, name_merchant_view;
    private String orderId;
    private LinearLayout finish;
    private Intent intent;
    private GoodsOrderDetail.Result toHttp;
    private DPHttpUtil.HttpCallback httpCallBack = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            if (0 == what) {
                toHttp = DPGsonUtil.fromJson(response, GoodsOrderDetail.Result.class);
                try {
                    JSONObject result = new JSONObject(response);
                    String contact_name = result.optString("contact_name");
                    String contact_phone = result.optString("contact_phone");
                    String address = result.optString("address");
                    String price_real = result.optString("price_real");
                    String name_merchant = result.optString("name_merchant");

                    int status = result.optInt("status");
                    if (3 == status) {
                        finish.setVisibility(View.VISIBLE);
                    } else if (100 == status) {
                        finish.setVisibility(View.GONE);
                    }

                    total_price.setText("实收款:  " + price_real + "元");
                    DPLogUtil.d("status==" + status);
                    contact_name_text.setText(contact_name);
                    contact_phone_text.setText(contact_phone);
                    address_text.setText(address);
                    name_merchant_view.setText(name_merchant);

                    JSONArray goods = result.optJSONArray("goods");
                    for (int i = 0; i < goods.length(); i++) {
                        HashMap<String, String> map = new HashMap<>();
                        map.put("name", goods.optJSONObject(i).optString("name"));
                        map.put("qty", goods.optJSONObject(i).optString("qty"));
                        map.put("price", goods.optJSONObject(i).optString("price"));
                        ListData.add(map);
                    }
                    detailGoodsAdapter = new DetailGoodsAdapter(DetailGoodsActivity.this, ListData);
                    goods_list.setAdapter(detailGoodsAdapter);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                CommonUtils.hideProgressDialog();
            }
            if (1 == what) {
                DPLogUtil.d("onSuccess==1==what");
                showToast("成功");
                CommonUtils.hideProgressDialog();
                finish(2);
            }

        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            if (0 == what) {
                CommonUtils.hideProgressDialog();
            }
            if (1 == what) {
                CommonUtils.hideProgressDialog();
            }

        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail_goods);
        setTitle("订单详情");
        goods_list = F(R.id.goods_list);
        finish = F(R.id.finish);
        contact_name_text = F(R.id.contact_name);
        contact_phone_text = F(R.id.contact_phone);
        address_text = F(R.id.address);
        total_price = F(R.id.total_price);
        name_merchant_view = F(R.id.name_merchant);

        intent = getIntent();
        orderId = intent.getStringExtra("id");
        DPLogUtil.d("getIntent" + orderId);
        ListData = new ArrayList<>();
        CommonUtils.showProgressDialog(DetailGoodsActivity.this);
        Map<String, String> data = new HashMap<>();
        data.put("token", CommonUtils.getToken(this));
        data.put("id", orderId);
        HttpUtil.httpPostWithParams(this, 0, Consts.API_GET_MALL_DETAIL, data, httpCallBack);
        finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtils.showProgressDialog(DetailGoodsActivity.this);
                Map<String, String> data = new HashMap<>();
                data.put("token", CommonUtils.getToken(DetailGoodsActivity.this));
                data.put("id", orderId);
                data.put("data", DPGsonUtil.toJson(toHttp));
                DPLogUtil.d(" DPGsonUtil.toJson(toHttp)" + DPGsonUtil.toJson(toHttp));
                HttpUtil.httpPostWithParams(DetailGoodsActivity.this, 1, Consts.API_GET_MALL_OK, data, httpCallBack);
            }
        });
    }

    public class DetailGoodsAdapter extends DPBaseAdapter<HashMap<String, String>> {
        private LayoutInflater inflater;

        public DetailGoodsAdapter(Context context) {
            super();
            init(context);
        }

        public DetailGoodsAdapter(Context context, List<HashMap<String, String>> data) {
            super(data);
            init(context);
        }

        private void init(Context context) {
            this.inflater = LayoutInflater.from(context);
        }

        @Override
        public CommonBaseViewHolder<HashMap<String, String>> getHolder(int position) {
            return new ViewHolder();
        }

        class ViewHolder extends CommonBaseViewHolder<HashMap<String, String>> {
            private TextView name;
            private TextView price;

            @Override
            public View create(int position) {
                View view = inflater.inflate(R.layout.item_detail_goods, null);
                name = (TextView) view.findViewById(R.id.name);
                price = (TextView) view.findViewById(R.id.price);
                return view;
            }

            @Override
            public void update(HashMap<String, String> data) {
                name.setText(data.get("name"));
                price.setText(data.get("qty"));

            }

        }
    }
}
